package com.bwei.like.mynewstop.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.activity.OffLineActivity;
import com.bwei.like.mynewstop.activity.RegistActivity;
import com.bwei.like.mynewstop.activity.SMSRegistActivity;
import com.bwei.like.mynewstop.adapater.Menu_ListView_Adapater;
import com.bwei.like.mynewstop.app.MyApplication;
import com.bwei.like.mynewstop.base.BaseFragment;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by LiKe on 2016/11/2.
 */
public class MenuFragment extends BaseFragment implements View.OnClickListener {

    private ListView listView;


    private ArrayList<Integer> pics = new ArrayList<>();
    private ImageButton ib_leftdraw_cellphone;
    private ImageButton ib_leftdraw_qq;
    private ImageButton ib_leftdraw_sina;
    private LinearLayout default_linearLayout;
    private LinearLayout success_linearLayout;
    private ImageView success_image;
    private TextView success_tv_person_name;
    private ImageView main_title_image;
    private InputStream inputStream;
    private ObjectInputStream objectInputStream;
    private Button bt_menu_offline;
    private Button bt_menu_night;
    private Button setTing;

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.sliding_memu_layout, null);
        //初始化控件
        infoView(view);
        pics.add(R.mipmap.dynamicicon_leftdrawer);
        pics.add(R.mipmap.topicicon_leftdrawer);
        pics.add(R.mipmap.love_jokebar_textpage_selected_pressed);
        pics.add(R.mipmap.activityicon_leftdrawer);
        pics.add(R.mipmap.sellicon_leftdrawer);
        pics.add(R.mipmap.feedbackicon_leftdrawer);
        pics.add(R.mipmap.baoliao);

        return view;

    }


    private void infoView(View view) {
        default_linearLayout = (LinearLayout) view.findViewById(R.id.ll_leftDrawer_login);
        success_linearLayout = (LinearLayout) view.findViewById(R.id.ll_leftDrawer_login_success);
        success_image = (ImageView) view.findViewById(R.id.iv_person_icon);
        success_tv_person_name = (TextView) view.findViewById(R.id.tv_person_name);
        main_title_image = (ImageView) getActivity().findViewById((R.id.main_title_image));
        listView = (ListView) view.findViewById(R.id.lv_leftDrawer);
        ib_leftdraw_cellphone = (ImageButton) view.findViewById(R.id.ib_leftdraw_cellphone);
        ib_leftdraw_cellphone.setOnClickListener(this);
        ib_leftdraw_qq = (ImageButton) view.findViewById(R.id.ib_leftdraw_qq);
        ib_leftdraw_qq.setOnClickListener(this);
        ib_leftdraw_sina = (ImageButton) view.findViewById(R.id.ib_leftdraw_sina);
        ib_leftdraw_sina.setOnClickListener(this);


        //最下面三个按钮
        bt_menu_offline = (Button) view.findViewById(R.id.bt_menu_offline);
        bt_menu_offline.setOnClickListener(this);
        bt_menu_night = (Button) view.findViewById(R.id.bt_menu_night);
        bt_menu_night.setOnClickListener(this);
        setTing = (Button) view.findViewById(R.id.setTing);
        setTing.setOnClickListener(this);
    }


    @Override
    protected void initActivity() {
        listView.setAdapter(new Menu_ListView_Adapater(pics, getActivity()));
        File cacheDir = MyApplication.getContext().getFilesDir();
        File file = new File(cacheDir,"loginQQ");

        if (cacheDir.exists()) {
            try {
                inputStream = new FileInputStream(file);
                objectInputStream = new ObjectInputStream(inputStream);
                Map<String, String> data = (Map<String, String>) objectInputStream.readObject();
                successLoginData(data);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null && objectInputStream != null) {
                        inputStream.close();
                        objectInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void onClick(View v) {
        //手机验证码监听
        switch (v.getId()) {
            case R.id.ib_leftdraw_cellphone:
                //跳转activity
                getActivity().startActivity(new Intent(getActivity(), SMSRegistActivity.class));
                getActivity().overridePendingTransition(R.anim.cell_regeist_in, R.anim.phone_out);
                break;

            //QQ登陆
            case R.id.ib_leftdraw_qq:
                UMShareAPI mShareAPI = UMShareAPI.get(getActivity());
//                mShareAPI.doOauthVerify(getActivity(), SHARE_MEDIA.QQ, umAuthListener);
                mShareAPI.getPlatformInfo(getActivity(), SHARE_MEDIA.QQ, umAuthListener);

                break;

            //微博登陆
            case R.id.ib_leftdraw_sina:

                break;
            //离线下载
            case R.id.bt_menu_offline:
                startActivity(new Intent(getActivity(), OffLineActivity.class));
                getActivity().overridePendingTransition(R.anim.cell_regeist_in, R.anim.phone_out);
                break;

            //夜间模式
            case R.id.bt_menu_night:

                break;

            //设置
            case R.id.setTing:

                break;
        }
    }


    //QQ监听
    private UMAuthListener umAuthListener = new UMAuthListener() {

        private OutputStream outputStream;
        private ObjectOutputStream objectOutputStream;

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            //   Toast.makeText(getActivity(), "Authorize succeed" + data.toString(), Toast.LENGTH_SHORT).show();
            successLoginData(data);

            File caFile = MyApplication.getContext().getFilesDir();
            File file = new File(caFile,"loginQQ");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
                try {
                    outputStream = new FileOutputStream(file);
                    objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(data);
                    objectOutputStream.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (objectOutputStream != null && outputStream != null) {

                            objectOutputStream.close();
                            outputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getActivity(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getActivity(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

    private void successLoginData(Map<String, String> data) {
        default_linearLayout.setVisibility(View.INVISIBLE);
        success_linearLayout.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().displayImage(data.get("profile_image_url"), success_image, CommonUtils.getInitOptions());
        ImageLoader.getInstance().displayImage(data.get("profile_image_url"), main_title_image, CommonUtils.getInitOptions());
        success_tv_person_name.setText(data.get("screen_name"));
    }
}
